package com.ssy.jt808.gateway.adapter.protocol;

import com.ssy.jt808.protocol.basics.JTMessage;
import com.ssy.jt808.protocol.commons.JT808;
import com.ssy.jt808.protocol.commons.transform.ParameterConverter;
import com.ssy.jt808.protocol.commons.transform.parameter.*;
import com.ssy.jt808.protocol.external.ParamAdasCalibration;
import com.ssy.jt808.protocol.external.ParamAdasCameraPosition;
import com.ssy.jt808.protocol.external.ParamVideoChannel;
import com.ssy.netmc.util.AdapterMap;
import io.github.yezhihao.protostar.annotation.Field;
import io.github.yezhihao.protostar.annotation.Message;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;

/**
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
@Data
@Message(JT808.设置终端参数)
public class T8103 extends JTMessage{

  @Field(length=1, desc="参数总数")
  private int                 total;
  @Field(desc="参数项列表", converter=ParameterConverter.class)
  private Map<Integer,Object> parameters;

  public T8103(){
  }

  public T8103( Map<Integer,Object> parameters ){
    this.parameters = parameters;
    this.total = parameters.size();
  }


  public T8103 addParameter( Integer key, Object value ){
    if( parameters == null ){
      parameters = new TreeMap();
    }
    parameters.put(key, value);
    total = parameters.size();
    return this;
  }

  @Schema(description="数值型参数列表(BYTE、WORD)")
  private Map<Integer,Integer>    parametersInt;
  @Schema(description="数值型参数列表(DWORD、QWORD)")
  private Map<Integer,String>     parametersLong;
  @Schema(description="字符型参数列表")
  private Map<Integer,String>     parametersStr;
  @Schema(description="图像分析报警参数设置(1078)")
  private ParamImageIdentifyAlarm paramImageIdentifyAlarm;
  @Schema(description="特殊报警录像参数设置(1078)")
  private ParamVideoSpecialAlarm  paramVideoSpecialAlarm;
  @Schema(description="音视频通道列表设置(1078)")
  private ParamChannels           paramChannels;
  @Schema(description="终端休眠唤醒模式设置数据格式(1078)")
  private ParamSleepWake          paramSleepWake;
  @Schema(description="音视频参数设置(1078)")
  private ParamVideo              paramVideo;
  @Schema(description="单独视频通道参数设置(1078)")
  private ParamVideoSingle        paramVideoSingle;
  @Schema(description="盲区监测系统参数(苏标)")
  private ParamBSD                paramBSD;
  @Schema(description="胎压监测系统参数(苏标)")
  private ParamTPMS               paramTPMS;
  @Schema(description="驾驶员状态监测系统参数(苏标)")
  private ParamDSM                paramDSM;
  @Schema(description="高级驾驶辅助系统参数(苏标)")
  private ParamADAS               paramADAS;

  /** 安易行设备 ADAS 标定信息 */
  private ParamAdasCameraPosition paramAdasCameraPosition;

  /** 安易行设备 ADAS 标定报像头位置 */
  private ParamAdasCalibration paramAdasCalibration;

  private ParamVideoChannel paramVideoChannel;

  public com.ssy.jt808.protocol.t808.T8103 build(){
    Map<Integer,Object> map = new TreeMap<>();

    if( parametersInt != null && !parametersInt.isEmpty() ){
      map.putAll(parametersInt);
    }

    if( parametersLong != null && !parametersLong.isEmpty() ){
      map.putAll(new AdapterMap(parametersLong, (Function<String,Long>)Long::parseLong));
    }
    if( parametersStr != null && !parametersStr.isEmpty() ){
      map.putAll(parametersStr);
    }
    if( paramADAS != null ){
      map.put(ParamADAS.key, paramADAS);
    }
    if( paramBSD != null ){
      map.put(ParamBSD.key, paramBSD);
    }
    if( paramChannels != null ){
      map.put(ParamChannels.key, paramChannels);
    }
    if( paramDSM != null ){
      map.put(ParamDSM.key, paramDSM);
    }
    if( paramImageIdentifyAlarm != null ){
      map.put(ParamImageIdentifyAlarm.key, paramImageIdentifyAlarm);
    }
    if( paramSleepWake != null ){
      map.put(ParamSleepWake.key, paramSleepWake);
    }
    if( paramTPMS != null ){
      map.put(ParamTPMS.key, paramTPMS);
    }
    if( paramVideo != null ){
      map.put(ParamVideo.key, paramVideo);
    }
    if( paramVideoSingle != null ){
      map.put(ParamVideoSingle.key, paramVideoSingle);
    }
    if( paramVideoSpecialAlarm != null ){
      map.put(ParamVideoSpecialAlarm.key, paramVideoSpecialAlarm);
    }

    if( paramAdasCalibration != null ){
      map.put(ParamAdasCalibration.KEY, paramAdasCalibration);
    }

    if( paramAdasCameraPosition != null ){
      map.put(ParamAdasCameraPosition.KEY, paramAdasCameraPosition);
    }

    if( paramVideoChannel != null ){
      map.put(ParamVideoChannel.KEY, paramVideoChannel);
    }

    if( CollectionUtils.isEmpty(this.parameters) ){
      this.total = map.size();
      this.parameters = map;
    }
    else{
      this.total += map.size();
      this.parameters.putAll(map);
    }

    com.ssy.jt808.protocol.t808.T8103 t8103 = new com.ssy.jt808.protocol.t808.T8103();
    t8103.setTotal(this.total);
    t8103.setParameters(this.parameters);
    t8103.setClientId(this.clientId);
    return t8103;
  }
}